package Algorithm;

import java.io.*;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.*;

public class RecommendedImpl implements Recommended {
    static final String DB_URL = "jdbc:mysql://39.105.0.127:3306/booksite";
    static final String USER = "root";
    static final String PASS = "123456";

    private DataHolder dataHolder;

    public List<String> getUser_tag_list() {
        return dataHolder.getUser_tag_list();
    }

    public void setUser_tag_list(List<String> user_tag_list) {
        dataHolder.setUser_tag_list(user_tag_list);
    }

    public List<String> getTag_item_list() {
        return dataHolder.getTag_item_list();
    }

    public void setTag_item_list(List<String> tag_item_list) {
        dataHolder.setTag_item_list(tag_item_list);
    }

    public Map<String, String> getRecolist_map() {
        return dataHolder.getRecolist_map();
    }

    public void setRecolist_map(Map<String, String> recolist_map) {
        dataHolder.setRecolist_map(recolist_map);
    }

    public List<String> getFinal_recolist() {
        return dataHolder.getFinal_recolist();
    }

    public void setFinal_recolist(List<String> final_recolist) {
        dataHolder.setFinal_recolist(final_recolist);
    }

    @Override
    public List<String> recommend(String userName) {
        dataHolder = new DataHolder();
        return getUserRecommendation(userName);
    }

    public List<String> getUserRecommendation(String user)
    {
        getWut(user);
        for (String l : getUser_tag_list()){
            String[] user_tag_count = l.split(",");
            String user_tag = user_tag_count[0];
            String wut = user_tag_count[1];
            if (user_tag != null){
                String tag_file_path = this.getClass().getResource("/").getPath()+"/Algorithm/mysql-files" + File.separator + user_tag + ".txt";
                getTagList(tag_file_path);
                System.out.println(tag_file_path);
                for (String item : getTag_item_list()){
                    String[] item_count = item.split("\\s+");
                    String item_id = item_count[1];
                    String wit = item_count[3];
                    int reco_weight = Integer.parseInt(wut) * Integer.parseInt(wit);
                    getRecolist_map().put(item_id, String.valueOf(reco_weight));
                }
            }
        }

        setRecolist_map(SortMap.sortMapByValueDesc(getRecolist_map()));
        for (Map.Entry<String, String> entry : getRecolist_map().entrySet()){
            getFinal_recolist().add(entry.getKey()+ ":" + entry.getValue());
        }
        return getFinal_recolist();
    }

    public List<String> getTagList(String tag_filepath)
    {
        try {
            File tag_file = new File(tag_filepath);
            if (tag_file.isFile() && tag_file.exists()){
                InputStreamReader inputStreamReader = new InputStreamReader(new FileInputStream(tag_file), "utf-8");
                BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
                String line = null;

                while ((line = bufferedReader.readLine()) != null){
                    getTag_item_list().add(line);
                }

                bufferedReader.close();
                inputStreamReader.close();
            }
            else {
                System.out.println("File not found!");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return getTag_item_list();
    }

    public int getWut(String user)
    {
        int wut = 0;

        Connection connection = null;
        Statement statement = null;
        try {
            Class.forName("com.mysql.jdbc.Driver");
            connection = DriverManager.getConnection(DB_URL,USER,PASS);
            statement = connection.createStatement();
            for (int i=1; i<7; i++){
                String user_tag = String.format("user_tag%d, ", i);
                String tag_mark = String.format("tag_mark%d) ", i);

                String sql = String.format("select concat_ws(',', user_tag%d, tag_mark%d) from user where user_name='%s';",i, i, user);

                ResultSet resultSet = statement.executeQuery(sql);
                while (resultSet.next()) {
                    String sql_result = resultSet.getString(1);
                    if (sql_result.contains(",")){
                        getUser_tag_list().add(sql_result);
                        System.out.println(sql_result);
                    }
                }
                resultSet.close();
            }
            // 完成后关闭
            statement.close();
            connection.close();

        } catch (Exception e){
            e.printStackTrace();
        }
        return wut;
    }

    class DataHolder{
        private List<String> user_tag_list = new ArrayList<>();
        private List<String> tag_item_list = new ArrayList<>();

        private Map<String, String> recolist_map = new TreeMap<>();
        private List<String> final_recolist = new ArrayList<>();

        public List<String> getUser_tag_list() {
            return user_tag_list;
        }

        public void setUser_tag_list(List<String> user_tag_list) {
            this.user_tag_list = user_tag_list;
        }

        public List<String> getTag_item_list() {
            return tag_item_list;
        }

        public void setTag_item_list(List<String> tag_item_list) {
            this.tag_item_list = tag_item_list;
        }

        public Map<String, String> getRecolist_map() {
            return recolist_map;
        }

        public void setRecolist_map(Map<String, String> recolist_map) {
            this.recolist_map = recolist_map;
        }

        public List<String> getFinal_recolist() {
            return final_recolist;
        }

        public void setFinal_recolist(List<String> final_recolist) {
            this.final_recolist = final_recolist;
        }
    }

}
